#include "monitor/cpu_load_monitor.h"

#include "utils/read_file.h"

#include "monitor_info.grpc.pb.h"
#include "monitor_info.pb.h"

namespace monitor
{
    void CpuLoadMonitor::UpdateOnce(monitor::proto::MonitorInfo *monitor_info)
    {
        ReadFile cpu_load_file(std::string("/proc/loadavg"));
        std::vector<std::string> cpu_load;
        cpu_load_file.ReadLine(&cpu_load);
        load_avg_1_ = std::stof(cpu_load[0]); // stof将字符串转化为浮点数
        load_avg_3_ = std::stof(cpu_load[1]);
        load_avg_15_ = std::stof(cpu_load[2]);

        // 填写proto中定义的MonitorInfo整个监控信息中的CpuLoad这一部分
        auto cpu_load_msg = monitor_info->mutable_cpu_load();
        cpu_load_msg->set_load_avg_1(load_avg_1_);
        cpu_load_msg->set_load_avg_3(load_avg_3_);
        cpu_load_msg->set_load_avg_15(load_avg_15_);

        return;
    }

    MonitorInter* CpuLoadFactory::CreateMonitor()
    {
        return new CpuLoadMonitor();
    }

} // namespace monitor